﻿using IniReadWrite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisionRoc.Core
{
    public class BlobAnalysis
    {
        public string BlobPath;

        /// <summary>
        /// Blob分析 参数路径
        /// </summary>
        string BlobFilePath;

        public BlobAnalysis(string _Path)
        {
            //判断文件路径是否存在
            if (!Directory.Exists(_Path))
            {
                Directory.CreateDirectory(_Path);
            }
            BlobFilePath = _Path + "\\Blob.ini";
            if (File.Exists(BlobFilePath))
            {
                //ReadfLinesParameter();
            }
        }

        [Browsable(true), Description("阈值分割最小值:\r\n  GreyMin>=0 & GreyMax<=255 & GreyMin<GreyMax"), Category("灰度阈值分割参数"), ReadOnly(false)]
        public double MinGray { get; set; }

        [Browsable(true), Description("阈值分割最大值:\r\n  GreyMin>=0 & GreyMax<=255 & GreyMax>GreyMin"), Category("灰度阈值分割参数"), ReadOnly(false)]
        public double MaxGray { get; set; }

        [Browsable(true), Description("特征提取最小值:\r\n  Min<Max"), Category("特征提取参数"), ReadOnly(false)]
        public double Min { get; set; }

        [Browsable(true), Description("特征提取最大值:\r\n  Max>Min"), Category("特征提取参数"), ReadOnly(false)]
        public double Max { get; set; }

        [Browsable(true), Description("筛选区域特性:\r\n  area:面积\r\n  circularity:圆度值\r\n  ra:椭圆的长半轴\r\n  rb:椭圆的短半轴\r\n  phi:椭圆的方向\r\n  contlength:区域的周长\r\n  max_diameter:区域最大外圆的直径\r\n  orientation:区域的方向"), TypeConverter(typeof(GenderItem_Six)), Category("特征提取参数"), ReadOnly(false)]
        public string Features { get; set; }

        public void ReadBlobParameter()
        {
            MinGray = Convert.ToDouble(IniAPI.INIGetStringValue(BlobFilePath, "阈值分割参数", "MinGray", "0"));
            MaxGray = Convert.ToDouble(IniAPI.INIGetStringValue(BlobFilePath, "阈值分割参数", "MaxGray", "0"));
            Min = Convert.ToDouble(IniAPI.INIGetStringValue(BlobFilePath, "阈值分割参数", "Min", "0"));
            Max = Convert.ToDouble(IniAPI.INIGetStringValue(BlobFilePath, "阈值分割参数", "Max", "0"));
            Features = IniAPI.INIGetStringValue(BlobFilePath, "阈值分割参数", "Features", "0");
        }

        public void WriteBlobParameter()
        {
            IniAPI.INIWriteValue(BlobFilePath, "阈值分割参数", "MinGray", MinGray.ToString());
            IniAPI.INIWriteValue(BlobFilePath, "阈值分割参数", "MaxGray", MaxGray.ToString());
            IniAPI.INIWriteValue(BlobFilePath, "阈值分割参数", "Min", Min.ToString());
            IniAPI.INIWriteValue(BlobFilePath, "阈值分割参数", "Max", Max.ToString());
            IniAPI.INIWriteValue(BlobFilePath, "阈值分割参数", "Features", Features);
        }
    }
    class GenderItem_Six : StringConverter
    {
        ////true 启用,false 禁用
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new string[] { "area", "circularity", "ra", "rb", "phi", "contlength", "max_diameter", "orientation" }); //编辑下拉框中的items
        }

        //true：禁用文本编辑，false：启用文本编辑
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
